var AInspectorChrome = AInspectorChrome || {};

AInspectorChrome.initialized = AInspectorChrome.initialized || false;
AInspectorChrome.imagesDisplayed = AInspectorChrome.imagesDisplayed || false;
AInspectorChrome.displayFrame = AInspectorChrome.displayFrame || null;
AInspectorChrome.popupDiv = AInspectorChrome.popupDiv || null;

/**
 * initializes the link cache 
 * 
 */
 
AInspectorChrome.init = function(){
	if(!AInspectorChrome.initialized){
		var script = document.createElement("script");
		script.setAttribute("src",chrome.extension.getURL("/helper.js"));
          document.head.appendChild(script);
		OpenAjax.a11y.cache.updateLinkCache( document );
		OpenAjax.a11y.cache.sortLinkCache('order');
		AInspectorChrome.initialized = true;
	}
}

/**
 * Adds an image to each link on the parent page along with a tooltip to convey information
 * 
 */
 
AInspectorChrome.insertImages = function(){
	// Create div to act as tooltip for links

	//Styling
	if(AInspectorChrome.popupDiv == null){
		AInspectorChrome.popupDiv = document.createElement("div");
		AInspectorChrome.popupDiv.id="popupdiv";
		AInspectorChrome.popupDiv.style.display='none';
		AInspectorChrome.popupDiv.style.maxWidth='250px';
		AInspectorChrome.popupDiv.style.maxHeight='150px';
		AInspectorChrome.popupDiv.style.wordWrap='break-word';
		AInspectorChrome.popupDiv.style.fontSize='12px';
		AInspectorChrome.popupDiv.style.border='1px solid red';
		AInspectorChrome.popupDiv.style.background='white';
		AInspectorChrome.popupDiv.style.padding='5px';
		AInspectorChrome.popupDiv.style.position='absolute';
		AInspectorChrome.popupDiv.style.left='0px';
		AInspectorChrome.popupDiv.style.right='0px';
		AInspectorChrome.popupDiv.style.zIndex = "2147483647";
		document.body.appendChild(AInspectorChrome.popupDiv);
	}
	
	if(!AInspectorChrome.imagesDisplayed){;
		//add event handler to each icon
		for (i in OpenAjax.a11y.cache.links) {
	     	var lo = OpenAjax.a11y.cache.links[i];
			
			 //overwriting the html code
	  	   	var newHTML = lo.node.innerHTML + "<img width=35 height=35 src='"+chrome.extension.getURL('/icons/failed_icon_mini.png')+"') onmousemove='displaymessage(event,\"<div><b>Name: </b>"+AInspectorChrome.clean(lo.node_text)+"</div><div><b>Link: </b>"+AInspectorChrome.clean(lo.href)+"</div><div><b>Alt Text: </b>"+AInspectorChrome.clean(lo.accessible_text)+"</div>\");' onmouseout='hidemessage();'/>";
	  		lo.node.innerHTML=newHTML;
		}
		AInspectorChrome.imagesDisplayed = true;
	}
}

AInspectorChrome.clean = function(str) {
	return str.replace('\"',"\\\"").replace('\'','\\\'').replace('\n','');
}


/**
 * Adds an iframe to the page, which can be later used to display information
 * 
 */
 
AInspectorChrome.addIFrame = function(){
	if(AInspectorChrome.displayFrame == null){
		AInspectorChrome.displayFrame = document.createElement("iframe");
		AInspectorChrome.displayFrame.setAttribute("scrolling", "yes");
		AInspectorChrome.displayFrame.style.background = "white";
		AInspectorChrome.displayFrame.style.position = "fixed";
		AInspectorChrome.displayFrame.style.zIndex = "2147483647";
		AInspectorChrome.displayFrame.style.top = "75%";
		AInspectorChrome.displayFrame.style.left = "0%";
		AInspectorChrome.displayFrame.style.width = "100%"; 
		document.body.appendChild(AInspectorChrome.displayFrame);
	}
}

/**
 * Adds the contents of the cache to a table within the iframe
 * 
 */
AInspectorChrome.addContent = function(){
	if(AInspectorChrome.displayFrame!=null){	
		var script = document.createElement("script");
		script.appendChild(document.createTextNode(""));
		document.head.appendChild(script);
		
		//create table
		var table =  AInspectorChrome.displayFrame.contentDocument.createElement("table");
		table.border = 1;
		var hrow = table.createTHead().insertRow(0);
		var hcell1 = hrow.insertCell(0);
		var hcell2 = hrow.insertCell(1);
		var hcell3 = hrow.insertCell(2);
		hcell1.innerHTML = "Link";
		hcell2.innerHTML = "URL";
		hcell3.innerHTML = "Alt Text";
		var tbod =  AInspectorChrome.displayFrame.contentDocument.createElement("tbody");
		table.appendChild(tbod);
		//add rows to tbody
		for (i in OpenAjax.a11y.cache.links) {
			var lo = OpenAjax.a11y.cache.links[i];
			if(lo.node.id == ''){
				//set a fake id if there isn't already an id
				//this should be removed if the highlight function that takes in a cache works
 				lo.node.setAttribute("id",("ainspector-fakeid-"+i));
			}
			var row = tbod.insertRow(0);
		//	row.setAttribute("onmouseover",("window.parent.highlight("i","+OpenAjax.a11y.cache+"');"));
			row.setAttribute("onmouseover",("window.parent.highlight('"+lo.node.id+"');"));
			row.setAttribute("onmouseout",("window.parent.removeHighlight('"+lo.node.id+"');"));
			var cell1 = row.insertCell(0);
			var cell2 = row.insertCell(1);
			var cell3 = row.insertCell(2);
			cell1.innerHTML = lo.node_text;
			cell2.innerHTML = lo.href;
			cell3.innerHTML = lo.accessible_text;
		}
		 AInspectorChrome.displayFrame.contentDocument.body.appendChild(table);
	}
}

AInspectorChrome.getCacheElement = function(i){
	return OpenAjax.a11y.cache.links[i];
}
